
<?php

function user_level_option($kode) {
    $ret = "";
    switch ($kode) {
        case 'admin' :
            $ret = '<option value="admin" selected>Administrator</option>';
            break;
        case 'member' :
            $ret = '<option value="member" selected>Member</option>';
            break;
    }

    return $ret;
}

function status_option($kode) {
    $ret = "";
    switch ($kode) {
        case 'Y' :
            $ret = '<option value="Y" selected>Aktif</option>';
            break;
        case 'N' :
            $ret = '<option value="N" selected>Tidak Aktif</option>';
            break;
    }

    return $ret;
}

function get_field_table_from_id($id, $field, $table, $prefix_id) {
    $sql = "SELECT $field FROM $table WHERE $prefix_id='$id' LIMIT 1";
    $hasil = mysql_query($sql);
    $baris = mysql_fetch_array($hasil);

    return $baris[$field];
}

function get_ukuran_select($nama, $pilihan) {
    $ret = "";
    $pil = "";
    switch ($pilihan) {
        case 's' :
            $pil = '<option value="s" selected>S</option>';
            break;
        case 'm' :
            $pil = '<option value="m" selected>M</option>';
            break;
        case 'l' :
            $pil = '<option value="l" selected>L</option>';
            break;
        case 'xl' :
            $pil = '<option value="xl" selected>L</option>';
            break;
    }

    $ret = " <select name='$nama'>" .
            $pil .
            "    <option value='s'>S</option>
	   	<option value='m'>M</option>
		<option value='x'>X</option>
		<option value='xl'>XL</option>
            </select>";

    return $ret;
}

function get_ongkir_select($nama) {
    $ret = "";

    $ret .= "<select name='$nama' id='$nama'>";
    $ret .= "<option value=\"0\">-- Pilih --</option>";

    $tampil = mysql_query("SELECT * FROM ongkirs ORDER BY ongkir_kota");
    while ($row = mysql_fetch_array($tampil)) {
        $ret .="<option value='" . $row["ongkir_id"] . "'>" . $row["ongkir_kota"] . " (Rp " . format_rupiah($row["ongkir_harga"]) . ")</option>";
    }
    $ret .= "</select>";

    return $ret;
}

function get_produk_gambar($kode) {
    $sql = "SELECT produk_gambar FROM produks WHERE produk_id='$kode' LIMIT 1";
    $hasil = mysql_query($sql);
    $baris = mysql_fetch_array($hasil);

    return $baris['produk_gambar'];
}

function get_ongkir_kota_from_id($id) {
    $sql = "SELECT ongkir_kota FROM ongkirs WHERE ongkir_id='$id' LIMIT 1";
    $hasil = mysql_query($sql);
    $baris = mysql_fetch_array($hasil);

    return $baris['ongkir_kota'];
}

function get_kategori_nama_from_id($id) {
    $sql = "SELECT kategori_nama FROM kategoris WHERE kategori_id='$id' LIMIT 1";
    $hasil = mysql_query($sql);
    $baris = mysql_fetch_array($hasil);

    return $baris['kategori_nama'];
}

function get_kategori_id_from_seo($id) {
    $sql = "SELECT kategori_id FROM kategoris WHERE kategori_seo='$id' LIMIT 1";
    $hasil = mysql_query($sql);
    $baris = mysql_fetch_array($hasil);

    return $baris['kategori_id'];
}

function get_kategori_seo_from_id($id) {
    $sql = "SELECT kategori_seo FROM kategoris WHERE kategori_id='$id' LIMIT 1";
    $hasil = mysql_query($sql);
    $baris = mysql_fetch_array($hasil);

    return $baris['kategori_seo'];
}

function get_kategori_link() {
    $sql = "SELECT kategori_id, kategori_nama, kategori_seo FROM kategoris ORDER BY kategori_seo ";
    $hasil = mysql_query($sql);
    $baris = mysql_fetch_row($hasil);

    echo "\n <ul>";
    do {
        list($kategori_id, $kategori_nama, $kategori_seo) = $baris;
        echo "<li><a href=\"" . URLHOST . "index.php?opr=kategori&amp;id=$kategori_seo\">$kategori_nama</a></li>";
    } while ($baris = mysql_fetch_row($hasil));
    echo "\n </ul>";
}

function get_produk_id_from_seo($id) {
    $sql = "SELECT produk_id FROM produks WHERE produk_seo='$id' LIMIT 1";
    $hasil = mysql_query($sql);
    $baris = mysql_fetch_array($hasil);

    return $baris['produk_id'];
}

function get_produk_name_from_seo($id) {
    $sql = "SELECT produk_nama FROM produks WHERE produk_seo='$id' LIMIT 1";
    $hasil = mysql_query($sql);
    $baris = mysql_fetch_array($hasil);

    return $baris['produk_nama'];
}

function get_produk_name_from_id($id) {
    $sql = "SELECT produk_nama FROM produks WHERE produk_id='$id' LIMIT 1";
    $hasil = mysql_query($sql);
    $baris = mysql_fetch_array($hasil);

    return $baris['produk_nama'];
}

function get_produk_field_from_id($id, $field) {
    $sql = "SELECT $field FROM produks WHERE produk_id='$id' LIMIT 1";
    $hasil = mysql_query($sql);
    $baris = mysql_fetch_array($hasil);

    return $baris[$field];
}

function get_produk_list($kategoris, $awalans, $halamans) {
    $perhalaman = $halamans;
    $halaman = isset($awalans) ? $awalans : "0";

    $awal = $halaman * $perhalaman;

    if ($kategoris != NULL) {
        $sql_produk = "SELECT produk_stok, produk_seo, produk_gambar, produk_nama, produk_harga FROM produks WHERE produk_kategori_id='" . $kategoris . "'  ORDER BY produk_id DESC LIMIT $awal, $perhalaman";
        //echo $sql_produk;
        $sql_jumlah = "SELECT count(produk_id) FROM produks WHERE  produk_kategori_id='" . $kategoris . "'";
        $seo = get_kategori_seo_from_id($kategoris);
        $links = URLPUBLICINDEX . "?opr=kategori&amp;id=$seo&amp;halaman=";
    } else {
        $sql_produk = " SELECT produk_stok,produk_seo, produk_gambar, produk_nama, produk_harga FROM produks  ORDER BY produk_id DESC LIMIT $awal, $perhalaman";
        //echo $sql_produk;
        $sql_jumlah = "SELECT count(produk_id) FROM produks";
        $links = URLPUBLICINDEX . "?opr=produk&amp;halaman=";
    }

    // produk
    $hasil_produk = mysql_query($sql_produk);
    $baris_produk = mysql_fetch_row($hasil_produk);

    //jumlah produk
    $hasil_jumlah = mysql_query($sql_jumlah);
    $baris_jumlah = mysql_fetch_row($hasil_jumlah);
    $total_halaman = ceil($baris_jumlah[0] / $perhalaman);
    $halaman_out = "";

    // jika berada pada halaman ketiga atau lebih, tampilkan link pertama
    if ($halaman > 1)
        $halaman_out .= "\n <li class=\"previous-off\"><a href=\"" . $links . "0\">&#171; First</a></li>";
    // jika berada pada halaman kedua atau lebih, tampilakn link sebelumnya
    if ($halaman > 0) {
        $hal = $halaman - 1;
        $halaman_out .= "\n <li class=\"previous-off\"><a href=\"" . $links . $hal . "\">< Previous</a></li>";
    }
    // ambil semua nomor halaman
    for ($i = 0; $i < $total_halaman; $i++) {
        $hal = $i + 1;
        if ($i == $halaman) {
            // jika halaman aktif, tidak usah diberi link 
            $halaman_out .= "\n <li class=\"active\">$hal</li>";
        } else {
            $halaman_out .= "\n <li><a href=\"" . $links . $i . "\">$hal</a></li>";
        }
    }
    // jika bukan berada pada halaman terakhir 
    if ($halaman < ($total_halaman - 1)) {
        $hal = $halaman + 1;
        $halaman_out .= "\n <li class=\"next\"><a href=\"" . $links . $hal . "\">Next ></a></li>";
    }
    // jika bukan pada 2 halaman terakhir 
    if ($halaman < ($total_halaman - 2)) {
        $hal = $total_halaman - 1;
        $halaman_out .= "\n <li class=\"next\"><a href=\"" . $links . $hal . "\">Last &#187;</a></li>";
    }
    // bentuk format halaman 
    $halaman_out = "<div id=\"ki_page\"><ul>$halaman_out</ul></div>";


    if ($baris_produk == false)
        lempar_ke_home();
    else {
        echo "<div id=\"ki_list\">";
        echo " <ul>";
        do {
            list($produk_stok, $produk_seo, $produk_gambar, $produk_nama, $produk_harga) = $baris_produk;
            $produk_harga = format_rupiah($produk_harga);
            echo "  <li>";
            echo "   <a href=\"" . URLPUBLICINDEX . "?opr=detail&amp;id=" . $produk_seo . "\">";
            echo "    <div class=\"ki_image\">";
            echo "     <img src=\"" . URLHOST . "media/foto_produk/small_" . $produk_gambar . "\" width=\"85px\" height=\"127px\" />";

            if ($produk_stok == 0) {
                echo "    <img src=\"" . URLHOST . "asset/img/" . "oos.png" . "\" width=\"50px\" height=\"50px\" />";
            }
            echo "    </div>";
            echo "    <h2>" . $produk_nama . "</h2>";
            echo "    <div class=\"ki_price\">Rp $produk_harga</div>";
            echo "   </a>";
            echo "  </li>";
        } while ($baris_produk = mysql_fetch_row($hasil_produk));
        echo " </ul>";
        echo "</div>";
    }

    echo "$halaman_out";
}

function get_produk_cari($kata, $awalans, $halamans) {
    $perhalaman = $halamans;
    $halaman = isset($awalans) ? $awalans : "0";

    $awal = $halaman * $perhalaman;

    $sql_produk = "SELECT produk_stok,produk_seo, produk_gambar, produk_nama, produk_harga FROM produks WHERE produk_nama like '%" . $kata . "%' or produk_deskripsi like '%" . $kata . "%' ORDER BY produk_id DESC LIMIT $awal, $perhalaman";
    //echo $sql_produk;
    $sql_jumlah = "SELECT count(produk_id) FROM produks WHERE produk_nama like '%" . $kata . "%' or produk_deskripsi like '%" . $kata . "%'";
    $links = URLPUBLICINDEX . "?opr=cari&amp;c=" . $kata . "&amp;halaman=";

    // produk
    $hasil_produk = mysql_query($sql_produk);
    $baris_produk = mysql_fetch_row($hasil_produk);

    //jumlah produk
    $hasil_jumlah = mysql_query($sql_jumlah);
    $baris_jumlah = mysql_fetch_row($hasil_jumlah);
    $total_halaman = ceil($baris_jumlah[0] / $perhalaman);
    $halaman_out = "";

    // jika berada pada halaman ketiga atau lebih, tampilkan link pertama
    if ($halaman > 1)
        $halaman_out .= "\n <li class=\"previous-off\"><a href=\"" . $links . "0\">&#171; First</a></li>";
    // jika berada pada halaman kedua atau lebih, tampilakn link sebelumnya
    if ($halaman > 0) {
        $hal = $halaman - 1;
        $halaman_out .= "\n <li class=\"previous-off\"><a href=\"" . $links . $hal . "\">< Previous</a></li>";
    }
    // ambil semua nomor halaman
    for ($i = 0; $i < $total_halaman; $i++) {
        $hal = $i + 1;
        if ($i == $halaman) {
            // jika halaman aktif, tidak usah diberi link 
            $halaman_out .= "\n <li class=\"active\">$hal</li>";
        } else {
            $halaman_out .= "\n <li><a href=\"" . $links . $i . "\">$hal</a></li>";
        }
    }
    // jika bukan berada pada halaman terakhir 
    if ($halaman < ($total_halaman - 1)) {
        $hal = $halaman + 1;
        $halaman_out .= "\n <li class=\"next\"><a href=\"" . $links . $hal . "\">Next ></a></li>";
    }
    // jika bukan pada 2 halaman terakhir 
    if ($halaman < ($total_halaman - 2)) {
        $hal = $total_halaman - 1;
        $halaman_out .= "\n <li class=\"next\"><a href=\"" . $links . $hal . "\">Last &#187;</a></li>";
    }
    // bentuk format halaman 
    $halaman_out = "<div id=\"ki_page\"><ul>$halaman_out</ul></div>";


    if ($baris_produk == false) {
        echo "\n <div id=\"ki_detail\">";
        echo "\n <p style=\"padding:5px; text-align:center;\">Data yang anda cari tidak ditemukan</p>";
        echo "\n </div>";
    } else {
        echo "<div id=\"ki_list\">";
        echo " <ul>";
        do {
            list($produk_stok, $produk_seo, $produk_gambar, $produk_nama, $produk_harga) = $baris_produk;
            $produk_harga = format_rupiah($produk_harga);
            echo "  <li>";
            echo "   <a href=\"" . URLPUBLICINDEX . "?opr=detail&amp;id=" . $produk_seo . "\">";
            echo "    <div class=\"ki_image\">";
            echo "     <img src=\"" . URLHOST . "media/foto_produk/small_" . $produk_gambar . "\" width=\"85px\" height=\"127px\" />";
            if ($produk_stok == 0) {
                echo "<div align=center>";
                echo "    <img src=\"" . URLHOST . "asset/img/" . "oos.png" . "\" width=\"50px\" height=\"50px\" />";
                echo "<div>";
            }
            echo "    </div>";
            echo "    <h2>" . $produk_nama . "</h2>";
            echo "    <div class=\"ki_price\">Rp $produk_harga</div>";
            echo "   </a>";
            echo "  </li>";
        } while ($baris_produk = mysql_fetch_row($hasil_produk));
        echo " </ul>";
        echo "</div>";
    }

    echo "$halaman_out";
}

function get_contact() {
    $aksi = "cms/bab/m_guest/aksi_guest.php";

    switch ($_GET["opr"]) {
        default:

            load_tinymce();

            echo "\n <br/>";
            echo "\n  <form method='POST' action='" . URLHOST . "$aksi?module=guest&opr=send' enctype='multipart/form-data'>";
            echo "\n <table>";
            echo "\n <tr><td>Nama Lengkap</td><td>:</td><td><input name='in_guest_nama' type='text'/></td></tr>";
            echo "\n <tr><td>Nomor Telepon</td><td>:</td><td><input name='in_guest_nope' type='text'/></td></tr>";
            echo "\n <tr><td>Email</td><td>:</td><td><input name='in_guest_email' type='text'/></td></tr>";
            echo "\n <tr><td>Katakan Sesuatu</td><td>:</td><td><textarea name = 'in_guest_isi' style = 'width: 300px; height: 100px;'></textarea></td></tr>";
            echo "\n <tr align='right'><td colspan='3'><input name='Submit' type='submit'  value='Send Request Form'/> 
		<input name='reset' type='reset' value='Reset Form'/> </td></tr></table>";
            echo "\n </form>";
            break;
    }
}

function get_produk_detail($id) {
    $sql = "SELECT * FROM produks where produk_id='" . $id . "'";
    $hasil = mysql_query($sql);
    $baris = mysql_fetch_array($hasil);

    if (mysql_num_rows($hasil) == 1) {
        load_js_validate();
        echo "\n <div id=\"ki_detail\">";
        echo "\n  <div id=\"kid_kiri\">";
        echo "\n   <div id=\"kidk_img\">";
        echo "\n    <img src=\"" . URLHOST . "media/foto_produk/" . $baris['produk_gambar'] . "\" width=\"178px\" height=\"247px\" />";
        if ($baris['produk_stok'] == 0) {
            echo "    <img src=\"" . URLHOST . "asset/img/" . "oos.png" . "\" width=\"50px\" height=\"50px\" />";
        }
        echo "\n    <h2>" . $baris['produk_nama'] . "</h2>";
        echo "\n   </div>";
        echo "\n   <div id=\"kidk_form\">";
        echo "\n    <form method=\"post\" action=\"" . URLHOST . "transaksi/perintah.php\">";
        echo "\n     <input type=\"hidden\" name=\"opr\" value=\"transaksi\" />";
        echo "\n     <input type=\"hidden\" name=\"id\" value=\"" . $baris['produk_id'] . "\" />";
        echo "\n     <label>Harga</label><p>Rp " . format_rupiah($baris['produk_harga']) . "</p>";
        echo "\n     <label>Diskon</label><p>" . $baris['produk_diskon'] . " %</p>";
        echo "\n     <label>Ukuran</label><select name=\"in_size\">";
        echo "\n       <option value=\"s\">S</option>";
        echo "\n       <option value=\"m\">M</option>";
        echo "\n       <option value=\"l\">L</option>";
        echo "\n       <option value=\"xl\">XL</option>";
        echo "\n      </select>";
        echo "\n     <label>Jumlah</label> <input type=\"text\" name=\"in_jumlah\" size=\"3\" maxlength=\"3\" value=\"1\" onkeypress=\"return isNumberKey(event)\" /><br />";
        echo "\n     <label></label>";
        echo "\n     <input type=\"submit\" value=\"\" />";
        echo "\n    </form>";
        echo "\n   </div>";
        echo "\n  </div>";
        echo "\n  <div id=\"kid_kanan\">";
        echo replace_url_img_from_admin_to_public($baris['produk_deskripsi']);
        echo "\n  </div>";
        echo "\n </div>";
    }
}

function get_produk_transaksi_total($id) {
    $sql = "SELECT sum(trt_jumlah) as total FROM tr_t WHERE trt_session='$id' LIMIT 1";
    $hasil = mysql_query($sql);
    $baris = mysql_fetch_array($hasil);

    return $baris['total'] == 0 ? '0' : $baris['total'];
}

function get_produk_transaksi_rupiah($id) {
    $sql = "SELECT sum(trt_harga) as total FROM tr_t WHERE trt_session='$id' LIMIT 1";
    $hasil = mysql_query($sql);
    $baris = mysql_fetch_array($hasil);

    return format_rupiah($baris['total']);
}

function get_basket_guest() {
    $sid = session_id();
    $aksi = "cms/bab/m_transaksi/trguest.php";
    echo "\n <div id=\"ki_detail\">";
    echo "\n  <div id=\"kid_kiri\">";
    echo "\n   <div id=\"kidk_img\">";
    echo "\n   </div>";
    echo "\n  </div>";
    echo "\n <div id=\"kid_kanan\">";
    echo " <table id=khusus>";
    echo "  <tr><th>No</th><th>Nama Produk</th><th>Gambar</th><th>Berat</th><th>Harga</th><th>Jumlah</th><th>Aksi</th></tr>";
    $p = new Paging;
    $batas = 50;
    $posisi = $p->cariPosisi($batas);
    $tampil = mysql_query("select * from temps, produks where temps.sess_id ='$sid' 
		and  temps.produk_id=produks.produk_id");
    $no = $posisi + 1;
    while ($baris = mysql_fetch_array($tampil)) {
        $harga = format_rupiah($baris["produk_harga"]);
        echo "<tr><td>$no</td>";
        echo " <td>" . $baris["produk_nama"] . "</td>";
        echo "<td><img src=\"" . URLHOST . "media/foto_produk/small_" . $baris['produk_gambar'] . "\" width=\"50px\" height=\"50px\" /> </td> ";
        echo " <td align='center'>" . $baris["produk_berat"] . "</td>";
        echo " <td> Rp. " . $harga . "</td>";
        echo " <td ><input align=center name=in_qty size=2 value=" . $baris["qty"] . "></td>";
        echo " <td><a href='" . URLHOST . "$aksi?module=basket&opr=del&id=" . $baris["temp_id"] . "'  onClick=\"return confirm('Hapus Beli Produk " . $baris["produk_nama"] . "?')\" ><img src=" . URLHOST . "media/img/kali.png border=0 title=Hapus></a>";
        echo " </td></tr>";
        $no++;
    }
    echo "</table>";

    echo "\n </div>";
}

//Profile
function get_statik_from_id($id) {
    $sql = "SELECT * FROM statiks where statik_id='" . $id . "'";
    $hasil = mysql_query($sql);
    $baris = mysql_fetch_array($hasil);

    if (mysql_num_rows($hasil) == 1) {
        echo "\n <div id=\"ki_detail\">";
        echo replace_url_img_from_admin_to_public($baris['statik_isi']);
        echo "\n </div>";
    }
}

function get_no_transaksi($tabel, $inisial) {
    $sql = mysql_query("SELECT * FROM $tabel");
    $field = mysql_field_name($sql, 1);
    $panjang = mysql_field_len($sql, 1);

    $qry = mysql_query("SELECT max(" . $field . ") FROM " . $tabel);
    $row = mysql_fetch_array($qry);
    if ($row[0] == "") {
        $angka = 0;
    } else {
        $angka = substr($row[0], strlen($inisial));
    }
    $angka++;
    $angka = strval($angka);
    $tmp = "";
    for ($i = 1; $i <= ($panjang - strlen($inisial) - strlen($angka)); $i++) {
        $tmp = $tmp . "0" < 6;
    }
    return $inisial . $tmp . $angka;
}

?>
